/**
 * 个人信息审批 - 表格显示新增，删除，修改项，新增项标红，修改前后不一致标红
 * A(ADD)  D(DELETE)  U(UPDATE)  B(BEFORE)  E(END)
 * @params :
 *    [ 
        {school:'北京大学',action:'A'}, 
        {school:'上海大学',action:'D'}, 
        {school:'江苏大学',action:'U',old:{school:'浙江大学'}},
        {school:'山西大学',action:'U',old:{school:'山东大学'}}
      ]
  * @returns : status为B表示修改前，status为E表示修改后 
      [ 
        {school:'北京大学',action:'A'}, 
        {school:'上海大学',action:'D'}, 
        {school:'浙江大学',status:'B'},
        {school:'江苏大学',action:'U',status:'E'},
        {school:'山东大学',status:'B'},
        {school:'山西大学',action:'U',status:'E'},
      ]
  * @actions : changePersonalApproveArr(params) === returns
  */
export function changePersonalApproveArr(arr) {
  let list = []
//1.往list中放入修改前的项
  arr.forEach((item,index) => {
    if(item.action === 'U'){
      item.status = 'E'
      item.old.status = 'B'
      item.old.index = index
      list.push(item.old)
    }
  })
//2.splice修改了arr，遍历插入元素需要考虑插入的索引，需要通过原arr中action为U时的索引与list中该项对应索引相加得出
  list.forEach((item,index) => {
    //splice(start,deleteCount,...item)
    arr.splice(item.index+index, 0, item)
  })
//3.去除每项中含old与index的属性
  arr.forEach(i=>{
    delete i.old
    delete i.index
  })
  return arr
}

/**
 *  对象转查询字符串
 * @params: {name:'张三',age:'2',sex:undefined}
 * @returns: ?name=张三&age=2
 */
export const getParams = data => {
  // 处理得到有值的对象
  const pageParam = (obj) => {
    const param = {};
    if (obj === null || obj === undefined || obj === '') return param;
    for (const key in obj) {
      if (obj[key] !== null && obj[key] !== undefined) {
        param[key] = obj[key];
      }
    }
    return param;
  };
  // 对象转查询字符串
  const params = pageParam(data);
  let paramsStr = '';
  for (const k in params) {
    if (params) {
      paramsStr += `${k}=${params[k]}&`;
    }
  }
  paramsStr = `?${paramsStr.substr(0, paramsStr.length - 1)}`;
  return paramsStr;
};


/**
 * 场景：h0单点认证
 *  params: #access_token=4427281e-7d81-4b59-a96e-d4a5f9a93998&token_type=bearer
 *  return: access_token: '4427281e-7d81-4b59-a96e-d4a5f9a93998', token_type: 'bearer'}
 */
export const transForm = () => {
  const hash = "#access_token=4427281e-7d81-4b59-a96e-d4a5f9a93998&token_type=bearer";
  // 方式1：使用reduce
  let params = hash.replace('#', '').split('&').reduce((total, item) => {
    // console.log(total, item);
    const newTotal = total;
    const index= item.indexOf('=');
    const key = item.slice(0, index);
    const val = item.slice(index + 1);
    newTotal[key] = val;
    return total;
  }, {});

  // 方式2：使用qs
  params = qs.parse(hash.replace('#', ''))
  console.log(params);  //{access_token: '4427281e-7d81-4b59-a96e-d4a5f9a93998', token_type: 'bearer'}
}

/**
 * 场景：h0单点认证地址（401时的跳转地址） redirect_uri之前的内容都是一样的，
 * 知识点： 解码decodeURIComponent('http%3A%2F%2F10.10.171.35%2Fhr')
 *         转码encodeURIComponent('http://10.10.171.35/hr ')
1.http://10.10.171.35:8080/oauth/oauth/authorize?response_type=token&client_id=localhost&redirect_uri=http%3A%2F%2F10.10.171.35%2Fhr
拼接的redirect_uri：http%3A%2F%2F10.10.171.35%2Fhr    
实际的              http://10.10.171.35/hr 
上述地址认证通过后链接为：
http://10.10.171.35/hr#access_token=4a3a97c7-3042-497d-a4f5-fb6b836c2467&token_type=bearer&expires_in=85512&refresh_token=ef3a8cf8-9435-42f6-a29b-6f50f1b694fa&scope=default
   
2.和第1个只是redirect_uri换了
拼接的redirect_uri：http%3A%2F%2F10.10.171.35%2Fmhr%2F%23%2FempRenewConfirm%3FrenewId%3D561
实际的：            http://10.10.171.35/mhr/#/empRenewConfirm?renewId=561
上述地址认证通过后链接为：
http://10.10.171.35/mhr/#/empRenewConfirm?renewId=561&access_token=4a3a97c7-3042-497d-a4f5-fb6b836c2467&token_type=bearer&expires_in=83976&refresh_token=ef3a8cf8-9435-42f6-a29b-6f50f1b694fa&scope=default

区别是不拼参数access_token前是#，拼参数access_token前是&
 *  
 */
export const transForm2 = () => {

}